<?php

namespace App\Jobs;

use App\Models\FlowChannel;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class VisitRecord implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */

    private $visit_info = '';

    public function __construct($visit_info)
    {
        $this->visit_info = $visit_info;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $visit_info = unserialize($this->visit_info);
        $time = Carbon::createFromTimestamp($visit_info['time']);


        $flow_channel = FlowChannel::query()->where('param', $visit_info['param'])->first();

        if(is_null($flow_channel)){
            $flow_channel_id = null;
        }else{
            $flow_channel_id = $flow_channel->id;
        }


        try {
            \App\Models\VisitRecord::query()->create([
                'order_id' => $visit_info['order_id'],
                'flow_channel_id' => $flow_channel_id,
                'ip' => $visit_info['ip'],
                'date' => $time->toDateString(),
                'hour' => $time->hour,
                'min_area' => intval($time->minute / 5),
                'visit_num' => $visit_info['visit_num'],
                'domain_id' => $visit_info['domain_id'],
                'u_id' => $visit_info['u_id'],
            ]);
        } catch (\Exception $exception) {

            Log::error('队列任务-》访问数据存入redis失败：' . $exception->getMessage().$this->visit_info);
        }
    }
}
